import { useEffect } from "react"
import useLatest from "./uselatest.ts"

/**
 * 组件卸载时执行的 hook
 * @param callback 组件卸载时执行的回调函数
 */
const useUnmount = (callback: () => void) => {
  const fnRef = useLatest(callback)
  useEffect(
    () => () => {
      fnRef.current()
    },

    // eslint-disable-next-line react-hooks/exhaustive-deps
    []
  )
}

export default useUnmount
